package com.tzy.newbank;

import com.tzy.newbank.Pojo.Banklog;
import com.tzy.newbank.Pojo.Bankuser;
import com.tzy.newbank.mapper.BanklogMapper;
import com.tzy.newbank.mapper.BankuserMapper;
import com.tzy.newbank.utils.NoPowerException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

@Slf4j
@SpringBootTest
public class mytest {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Test
    public void saveToRedis(){
        redisTemplate.opsForValue().set("name","tzy",2, TimeUnit.MINUTES);
    }

    @Test
    public void getFromRedis(){
        log.info("从redis中获取数据");
        System.out.println(redisTemplate.opsForValue().get("name"));
    }


    @Autowired
    private BankuserMapper bankuserMapper;
    @Autowired
    private BanklogMapper banklogMapper;


    @Test
    public void test0() {
        try {
            throw new NoPowerException();
        } catch (NoPowerException e) {
            Logger.getLogger("异常").log(Level.SEVERE, "发生了一个异常", e);
        }
    }

    @Test
    public void test1(){
        Bankuser bankuser = new Bankuser();
        bankuser.setId(2);
        bankuser.setName("tt");
        bankuser.setBalance(333.0);
        bankuser.setState(0);
        bankuser.setPassword("tt");
        Double money=22.0;
        Bankuser bankuser2 = new Bankuser();
        bankuser2.setName("ttt");
        deposit2(bankuser,77.0);
    }

    public Boolean deposit2(Bankuser bankuser, Double money) {
        bankuserMapper.deposit(money,bankuser.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(),"存钱："+money, LocalDateTime.now()));
        return true;
    }

    public Boolean withdrawals2(Bankuser bankuser, Double money) {
        bankuserMapper.withdraw(money,bankuser.getName());
        banklogMapper.saveLog(new Banklog(bankuser.getName(),"取钱："+money, LocalDateTime.now()));
        return true;
    }

    public void mytry(Bankuser bankuser) {
        bankuserMapper.insertAll(bankuser);
    }

    public Boolean register(Bankuser bankuser) {
        bankuserMapper.insert(bankuser);
        return true;
    }

    public Boolean deposit(Bankuser bankuser, Double money) {
        bankuserMapper.deposit(money,bankuser.getName());
        return true;
    }

    public Boolean withdraws(Bankuser bankuser, Double money) {
        bankuserMapper.withdraw(money,bankuser.getName());
        return true;
    }

    public Boolean transfer(Bankuser bankuser, Bankuser bankuser2, Double money) {
        bankuserMapper.withdraw(money,bankuser.getName());
        bankuserMapper.deposit(money,bankuser2.getName());
        return true;
    }

    public void inquiry(Bankuser bankuser) {
        System.out.println(bankuserMapper.selectByName(bankuser.getName()));
    }

    public boolean login2(Bankuser bankuser) {
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        if (bankuser.getPassword().equals(bankuser1.getPassword())) {
            bankuser.setBalance(bankuser1.getBalance());
            System.out.println("true");
            return true;
        }
        System.out.println("false");
        return false;
    }
    public void login(Bankuser bankuser) {
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        System.out.println(bankuser.getPassword().equals(bankuser1.getPassword()));
    }

    public Boolean destroyAccount(Bankuser bankuser) {
        bankuserMapper.deleteByName(bankuser.getName());
        return true;
    }

    public void exist(Bankuser bankuser) {
        Bankuser bankuser1 = bankuserMapper.selectByName(bankuser.getName());
        System.out.println(bankuser1!=null);
    }
}
